﻿using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DiscoveryLogic.UI.Charts
{
    [ScriptableType]
    public class ToolTip : VisualElement
    {
        #region fields 
        internal Border _borderElement;
        private TextBlock _toolTipTextBlock;
        #endregion

        public ToolTip()
        {
            Text = "";
            DefaultStyleKey = typeof(ToolTip);
        }

        #region props
        public static DependencyProperty EnabledProperty =
            DependencyProperty.Register("Enabled",
            typeof(Nullable<bool>), typeof(ToolTip), null);

        [TypeConverter(typeof(NullableBoolConverter))]
        public Nullable<bool> Enabled
        {
            get
            {
                if ((Nullable<Boolean>)GetValue(EnabledProperty) == null)
                {
                    if (Chart != null)
                        return Chart.ToolTipEnabled;
                    else
                        return true;
                }
                else
                {
                    return (Nullable<Boolean>)GetValue(EnabledProperty);
                }
            }
            set { base.SetValue(EnabledProperty, value); }
        }

        public static DependencyProperty TextProperty =
            DependencyProperty.Register("Text",
            typeof(string), typeof(ToolTip), null);

        public string Text
        {
            get
            {
                return (GetValue(TextProperty) == null) ? "" : (String)GetValue(TextProperty);
            }
            set
            {
                String val = ObservableObject.GetFormattedMultilineText(value);
                SetValue(TextProperty, val);
            }
        }

        public static DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius",
            typeof(CornerRadius), typeof(ToolTip), null);

        public CornerRadius CornerRadius
        {
            get { return ((CornerRadius)base.GetValue(CornerRadiusProperty)); }
            set { base.SetValue(CornerRadiusProperty, value); }
        }

        public static DependencyProperty FontColorProperty =
            DependencyProperty.Register("FontColor",
            typeof(Brush), typeof(ToolTip), null);

        public Brush FontColor
        {
            get { return ((Brush)base.GetValue(FontColorProperty)); }
            set { base.SetValue(FontColorProperty, value); }
        }

        public VisualControl Chart
        {
            get;set;
        }
        #endregion

        #region pub/internal methods
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _borderElement = GetTemplateChild("ToolTipBorder") as Border;
            _toolTipTextBlock = GetTemplateChild("ToolTipTextBlock") as TextBlock;
            this.Visibility = Visibility.Collapsed;
        }

        internal void Show()
        {
            if ((Boolean)Enabled)
            {
                if (Text == String.Empty)
                    Hide();
                else
                    this.Visibility = Visibility.Visible;
            }
            else
                Hide();
        }

        internal void Hide()
        {
            this.Visibility = Visibility.Collapsed;
        }
        #endregion

        #region private methods
        private static void OnEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ToolTip t = d as ToolTip;

            if (t._borderElement != null)
            {
                if ((Boolean)e.NewValue)
                    t._borderElement.Visibility = t.Visibility;
                else
                    t._borderElement.Visibility = Visibility.Collapsed;
            }
        }

        private static void OnTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ToolTip tootlTip = d as ToolTip;

            if (tootlTip._toolTipTextBlock != null)
                tootlTip._toolTipTextBlock.Text = (String)e.NewValue;

            if (tootlTip.Chart != null)
            {
                tootlTip.MaxWidth = tootlTip.Chart.ActualWidth;

                if (tootlTip._toolTipTextBlock != null)
                    tootlTip._toolTipTextBlock.MaxWidth = tootlTip.Chart.ActualWidth - 4;
            }
        }
        #endregion
    }
}
